package Boundry;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JPanel;

import Boundry.GenericTools.GenericTabel;
import Boundry.GenericTools.IPanelOperation;
import Boundry.GenericTools.InputPanel;
import Controllers.GUIControllerICMManager;
import Controllers.GlobalData;
import Controllers.GuiManager;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

public class ShowWorkersPanel extends InputPanel implements IPanelOperation 
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GenericTabel genericTabel;
	private JPanel tablepanel;
	private JTextField tfworkerName;
	private JTextField tfEmail;
	private JTextField tfPhone;
	private JTextField tfRole;
	private JTable table;
	private DefaultTableModel model;
	private JPanel tablePanel;
	private JLabel lblWorkIn;
	private ListSelectionModel cellSelectionModel;
	




	public ShowWorkersPanel()
	{
		int Width=358, Height=316;
		tablepanel = new JPanel();
		tablepanel.setBounds(30, 33, Width, Height);
		tablepanel.setLayout(null);
		mainPanel.add(tablepanel);
		
		genericTabel = new GenericTabel(Width-30, Height-30, "Worker List");
		tablepanel.add(genericTabel);
		
		JPanel panel = new JPanel();
		panel.setBounds(483, 33, 374, 316);
		mainPanel.add(panel);
		panel.setLayout(null);
		
		JLabel lblWorkerName = new JLabel("Worker Name");
		lblWorkerName.setBounds(10, 29, 92, 14);
		panel.add(lblWorkerName);
		
		tfworkerName = new JTextField();
		tfworkerName.setBounds(150, 26, 127, 20);
		panel.add(tfworkerName);
		tfworkerName.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Email");
		lblNewLabel.setBounds(10, 60, 76, 14);
		panel.add(lblNewLabel);
		
		tfEmail = new JTextField();
		tfEmail.setBounds(150, 57, 127, 20);
		panel.add(tfEmail);
		tfEmail.setColumns(10);
		
		JLabel lblPhone = new JLabel("Phone");
		lblPhone.setBounds(10, 96, 92, 14);
		panel.add(lblPhone);
		
		tfPhone = new JTextField();
		tfPhone.setBounds(150, 93, 127, 20);
		panel.add(tfPhone);
		tfPhone.setColumns(10);
		
		JLabel lblWorkerRoll = new JLabel("Worker Role");
		lblWorkerRoll.setBounds(10, 135, 76, 14);
		panel.add(lblWorkerRoll);
		
		tfRole = new JTextField();
		tfRole.setBounds(150, 132, 127, 20);
		panel.add(tfRole);
		tfRole.setColumns(10);
		
		lblWorkIn = new JLabel("Worker is Assign  % numbers of request");
		lblWorkIn.setBounds(25, 200, 252, 14);
		panel.add(lblWorkIn);
		
	      tablePanel = new JPanel();
	      tablePanel.setBounds(25, 225, 317, 64);
	      panel.add(tablePanel);
	
		 model = new DefaultTableModel(); 
		table = new JTable(model) 
		{
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int vColIndex) {
	  	        return false;
	  	      }
			public boolean getScrollableTracksViewportWidth()
	        {
	            return getPreferredSize().width < getParent().getWidth();
	        }
		};
	  	  table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
	  	  table.getTableHeader().setReorderingAllowed(false);
	  	  
	  	  table.setGridColor(Color.black);
	      JScrollPane scrollPane = new JScrollPane(table);
	      scrollPane.setPreferredSize(new Dimension(317, 64));  
	      tablePanel.add(scrollPane);
	      
	      
	      String[] col = new String[]{"Inspector","Evalutor","Execution Leader","ChairMan","Commtiee Member","Examinar"};
	      String[] data =  new String[]{"0","0","0","0","0","0"};
	      buildTabel(col,data);
	      
	      
	
	      cellSelectionModel = genericTabel.getCellSelectionModel();
			
	}
	
	
	public JTable getTable() {
		return genericTabel.getTable();
	}
	
	
	public ListSelectionModel getCellSelectionModel() 
	{
		return cellSelectionModel;
	}
	public void setWorkerName(String name)
	{
		tfworkerName.setText(name);
	}
	public void setEmail(String name)
	{
		tfEmail.setText(name);
	}
	public void setPhone(String name)
	{
		tfPhone.setText(name);
	}
	public void setRole(String name)
	{
		tfRole.setText(name);
	}
	public void setNumberOfRequest(int num)
	{
		lblWorkIn.setText("Worker is Assign " + num + " numbers of request");
	}
	
	@Override
	public String apHelpButton() {
 		return "Here the ICM Manager can look at all the workers listed in the system";
	}

	@Override
	public String setTitle() {
		return "Show Workers in the system";
	}
	
	public void buildTabel(String[] columnNames, String[][] tableData)
	{   
		genericTabel.buildTabel(columnNames,tableData);
	}
	

	
	/*
	 * Table of Worker request numbers
	 */
	public void addRow(String[] row)
	{
		 model.addRow(row);	
	}
	public void buildTabel(String[] columnNames, String[] tableData)
	{   
		if(model.getRowCount()>0) removeAllRows();
		
		int length = columnNames.length;
		for(int i=0;i<length;i++)
			addColumn(columnNames[i]); 
		
		addRow(tableData);

    	System.out.println("In new Table");
	}
	public void addColumn(String colName)
	{
		model.addColumn(colName);
	}
	public void removeRow(int rowLineNumber)
	{
		if(model.getRowCount()>rowLineNumber)
		{
		   model.removeRow(rowLineNumber);
		}
	}
	public void removeAllRows()
	{
		int numberOfRows= model.getRowCount();
        
		for(int i = 0;i<numberOfRows;i++)
        {
            model.removeRow(0);
        } 
	}
	
	
	public static void main(String[] args){
		JFrame frame =  new JFrame();
		frame.setVisible(true);
		//frame.setSize(GlobalData.FrameWidth, GlobalData.FrameHeight);
		
		frame.getContentPane().add(new ShowWorkersPanel());
		
	}
}
